package bom.example.common.swagger;

import bom.example.common.utils.PropertiesUtils;
import bom.example.common.utils.StringUtils;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;


import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;



/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/6/6 0:02
 * @since JDK 1.8
 *
 * 访问地址 127.0.0.1:8085/doc.html
 */
@Configuration
@EnableSwagger2 // 启用swagger2
public class SwaggerConfig {

    /**
     * 定义分隔符
     */
    private static final String splitor = ";";

    @org.springframework.context.annotation.Bean
    public Docket createRestApi() {
        String basePackages=getBasePackages();
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        java.util.List<Parameter> parameters = new ArrayList<>();
        parameterBuilder.name("Authorization").description("令牌").modelRef(new ModelRef("string")).parameterType("header")
              .required(false).build();
       parameters.add(parameterBuilder.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
               // .apis(basePackage(basePackages))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(parameters);

    }


    public String getBasePackages(){
        List<String> strings=new ArrayList<>();
        PropertiesUtils.loadProperties("swagger",strings);
        String str=StringUtils.StringFormart(strings,splitor);
        return str;
    }

    /**
     * 声明基础包
     *
     * @param basePackage 基础包路径
     * @return
     */
    public static Predicate<RequestHandler> basePackage(final String basePackage) {
        return input -> declaringClass(input).transform(handlerPackage(basePackage)).or(true);
    }

    /**
     * 校验基础包
     *
     * @param basePackage 基础包路径
     * @return
     */
    private static Function<Class<?>, Boolean> handlerPackage(final String basePackage) {
        return input -> {
            for (String strPackage : basePackage.split(splitor)) {
                boolean isMatch = input.getPackage().getName().startsWith(strPackage);
                if (isMatch) {
                    return true;
                }
            }
            return false;
        };
    }

    /**
     * 检验基础包实例
     *
     * @param requestHandler 请求处理类
     * @return
     */
    @SuppressWarnings("deprecation")
    private static Optional<? extends Class<?>> declaringClass(RequestHandler requestHandler) {
        return Optional.fromNullable(requestHandler.declaringClass());
    }





    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("springboot")
                .description("swagger-bootstrap-ui")
                .termsOfServiceUrl("http://localhost:8085/")
                .contact("2532223478@mail.com")
                .version("1.0")
                .build();
    }
}
